<?php
/**
 * Created by PhpStorm.
 * User: softyizhi
 * Date: 2017/6/1
 * Time: 下午7:18
 * 版本信息管理
 */

namespace Admin\Controller;


use Base\Controller\CurdController;
use Library\UploadFile;

class AppversioninfoController extends CurdController
{
   public function __construct()
    {
        parent::__construct(true, true, array(
            'input_name' => 'downLoadURL',//文本框name
            'save_path' => 'package',//保存路径
            'data_field' => 'downLoadURL'//数据库字段
        ));
     }
    /**
     * 获取所有数据
     */
    protected function getAll()
    {
        if($_REQUEST['keyword']){
            $where['Packages'] = array('like','%'.$_REQUEST['keyword'].'%');
        }
        if(!I('get.del')){
            $where['status']=1;
        }else{
            $where['status']=0;
        }
        if($_REQUEST['selects']){
            $limit=$_REQUEST['selects'];
        }else{
            $limit=20;
        }
        $data = $this->model->pageToView($limit, true,$where);
        $this->assign("page", $data['page']);
        $this->assign("list", $data['list']);
    }

    /**
     * 添加
     */
    public function add()
    {
        $data = $this->model->create();
        if (!$data) {
            $this->error($this->model->getError());
        } else {
            if ($this->upload) {
                //上传安装包
                if ($_FILES['downLoadURL']) {
                    $name=C('roots');
                    $up = new UploadFile();
                    $info = $up->upFile('apk', array('apk', 'ipa'));
                    $datas= $info['downLoadURL']['savepath'] . $info['downLoadURL']['savename'];
                    $data['downLoadURL'] = 'http://'.$_SERVER["HTTP_HOST"].'/'.$name.'/Uploads/'. $datas;
                }
            }
            $data = $this->addAfter($data);
            $add = $this->model->add($data);
            $this->addEnd($add);
        }
    }
    protected function addAfter($data)
    {
        $ids=$this->model->order('id desc')->find();
        $data['id']=$ids['id']+1;
        return $data;
    }
    /**
     * 彻底删除
     * @param string $id 要删除的id，多个以,分割
     */
    public function deleted($id)
    {
        $arr = explode(',', $id);
        $i = 0;
        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
            $data = $this->model->find($id);
            if ($data['packages']) {
                //删除文件
                $upConfig = C('UPLOADS');
                removeFile($upConfig['PATH'] . '/' . $data['packages']);
            }
            $this->model->delete($v);
            $i++;
        }
        $this->success('彻底删除' . $i . '条数据', $this->urlConfig['del_success']);
    }

}